package com.stone.persistent.recyclerview.adapter.viewholder;

import android.graphics.Color;
import android.text.TextPaint;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.stone.persistent.recyclerview.R;
import com.stone.persistent.recyclerview.adapter.FeedsPagerAdapter;
import com.stone.persistent.recyclerview.myApplication;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class FeedsViewHolder extends RecyclerView.ViewHolder {
    private static final String TAG = "FeedsViewHolder";
    private List<TextView> tabList = new ArrayList();
    private ViewPager2 viewPager2;

    private final void bindTabs() {

        View.OnClickListener tabClickListener = (new View.OnClickListener() {
            public final void onClick(View view) {
                int index = tabList.indexOf(view);
                viewPager2.setCurrentItem(index);
            }
        });


        tabList.add((TextView) itemView.findViewById(R.id.feeds_tab1));
        tabList.add((TextView) itemView.findViewById(R.id.feeds_tab2));
        tabList.add((TextView) itemView.findViewById(R.id.feeds_tab3));
        tabList.add((TextView) itemView.findViewById(R.id.feeds_tab4));
        tabList.add((TextView) itemView.findViewById(R.id.feeds_tab5));
        Iterator var3 = this.tabList.iterator();

        while (var3.hasNext()) {
            TextView itemTab = (TextView) var3.next();
            itemTab.setOnClickListener(tabClickListener);
        }

        viewPager2.registerOnPageChangeCallback((new ViewPager2.OnPageChangeCallback() {
            public void onPageSelected(int position) {
                FeedsViewHolder.this.onTabChanged(position);
            }
        }));
        this.onTabChanged(0);
    }

    private final void onTabChanged(int position) {
        int num = this.tabList.size();
        int i = 0;

        for (int var4 = num; i < var4; ++i) {
            TextView itemTab = (TextView) this.tabList.get(i);
            TextPaint var6;
            if (i == position) {
                itemTab.setTextColor(Color.parseColor("#ff0000"));
                var6 = itemTab.getPaint();
                var6.setFakeBoldText(true);
                itemTab.setTextSize(2, 18.0F);
            } else {
                itemTab.setTextColor(Color.parseColor("#333333"));
                var6 = itemTab.getPaint();
                var6.setFakeBoldText(false);
                itemTab.setTextSize(2, 15.0F);
            }
        }

    }

    //
    public void initAdapter(AppCompatActivity activity) {
        if (myApplication.getContext().isRefesh) {
            viewPager2.setAdapter(new FeedsPagerAdapter(activity));
            myApplication.getContext().isRefesh = false;
        }
    }


    public FeedsViewHolder(@NotNull View itemView) {
        super(itemView);
        Log.e(TAG, "FeedsViewHolder: ");
        viewPager2 = (ViewPager2) itemView.findViewById(R.id.main_feeds_view_pager);
        viewPager2.setOrientation(viewPager2.ORIENTATION_HORIZONTAL);
        this.bindTabs();
    }


}